package com.zyb.insurance.policy.controller;


import com.alibaba.fastjson.JSONObject;
import com.zyb.insurance.common.HBaseClient;
import com.zyb.insurance.policy.entity.InsurancePolicy;
import com.zyb.insurance.policy.entity.InsurancePolicyFactory;
import com.zyb.insurance.policy.entity.SearchParameters;
import com.zyb.insurance.policy.infrastructure.ElasticsearchManager;
import com.zyb.insurance.policy.infrastructure.HBaseManager;
import com.zyb.insurance.policy.infrastructure.RedisManager;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.TableNotEnabledException;
import org.apache.hadoop.hbase.client.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 保单数据管理模块web请求控制器
 *
 * @author dhh
 */
@RestController
@RequestMapping("/insurance/policy")
public class InsurancePolicyController {

    @Autowired
    private HBaseManager hBaseManager;
    @Autowired
    private RedisManager redisManager;
    @Autowired
    private ElasticsearchManager elasticsearchManager;

    public InsurancePolicyController() {
        hBaseManager.createInsurancePolicyTableIfNotExists();
    }

    /**
     * 创建保单
     * @return
     */
    @GetMapping("/create")
    public String create() {
        try {
            InsurancePolicy insurancePolicy = InsurancePolicyFactory.build();
            hBaseManager.saveInsurancePolicyIntoHbase(insurancePolicy);
            redisManager.buildInsurancePolicyPageIndex(insurancePolicy.getInsurancePolicyId());
            // 基于ES构建倒排索引
            elasticsearchManager.buildInsurancePolicyIndex(insurancePolicy);
            // 以及基于ES去进行复杂搜索+分页
        } catch(Exception e) {
            e.printStackTrace();
            return "failure";
        }

        return "success";
    }

    /**
     * 分页查询保单数据
     * @param page
     * @return
     */
    @GetMapping("/page/{page}")
    public String queryByPage(@PathVariable("page") int page) {
        try {
            List<String> insurancePolicyIds = redisManager.queryInsurancePolicyByPage(page);
            Integer insurancePolicyCount = redisManager.queryInsurancePolicyCount();
            System.out.println("保单分页id集合：" + insurancePolicyIds);
            System.out.println("保单总数量：" + insurancePolicyCount);

            for(String insurancePolicyId : insurancePolicyIds) {
                hBaseManager.queryInsurancePolicyFromHBase(insurancePolicyId);
            }
        } catch(Exception e) {
            e.printStackTrace();
            return "failure";
        }
        return "success";
    }

    /**
     * 查询保单
     * @param insurancePolicyId
     */
    @GetMapping("/{insurancePolicyId}")
    public String queryById(@PathVariable("insurancePolicyId") String insurancePolicyId) {
        try {
            hBaseManager.queryInsurancePolicyFromHBase(insurancePolicyId);
        } catch(Exception e) {
            e.printStackTrace();
            return "failure";
        }
        return "success";
    }

    /**
     * 搜索保单
     */
    @GetMapping("/search")
    public String search(String contractCode) {
        try {
            System.out.println("合同编号：" + contractCode);
            SearchParameters searchParameters = new SearchParameters();
            searchParameters.setContractCodeOrProposalFormCode(contractCode);
            searchParameters.setFrom(0);
            searchParameters.setSize(20);
            elasticsearchManager.search(searchParameters);
        } catch(Exception e) {
            e.printStackTrace();
            return "failure";
        }
        return "success";
    }
}
